<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of StoriesController
 *
 * @author Zoila
 */
class WebChatsController extends AppController{
    
    //AUTORIZACION
    public function isAuthorized($user) {
        // All registered users can add posts
        if (in_array($this->action, array('ajax','chatRoom'))) {
            return true;
        }

        // The owner of a post can edit and delete it
        if (in_array($this->action, array('delete'))) {
            $stId = $this->request->params['pass'][0];
            if ($this->WebChat->isOwnedBy($stId, $user['id'])) {
                return true;
            }
                return true;
        }

        return parent::isAuthorized($user);
    }
    
    public function chatRoom(){
//        $this->autoLayout=false;
//        $this->Layout='ajax';
        $this->layout='chat';
    }
    
     public function ajax(){
        $this->RequestHandler->setContent('json', 'WebChats/json' ); 
//        require "classes/DB.class.php";
//        require "classes/Chat.class.php";
//        require "classes/ChatBase.class.php";
//        require "classes/ChatLine.class.php";
//        require "classes/ChatUser.class.php";

//        session_name('webchat');
//        session_start();
//
//        if(get_magic_quotes_gpc()){
//
//                // If magic quotes is enabled, strip the extra slashes
//                array_walk_recursive($_GET,create_function('&$v,$k','$v = stripslashes($v);'));
//                array_walk_recursive($_POST,create_function('&$v,$k','$v = stripslashes($v);'));
//        }

        try{


                $response = array();

                // Handling the supported actions:

                switch($_GET['action']){

                        case 'login':
                            $response = array(
                                                'status'	=> 1,
                                                'name'		=> $this->Auth->user('username'),
                                                'gravatar'	=> $this->Auth->user('gravatar')
                                        );
                        break;

                        case 'checkLogged':
                                $response = Chat::checkLogged();
                        break;

                        case 'logout':
                                $response = Chat::logout();
                        break;

                        case 'submitChat':
                                $response = Chat::submitChat($_POST['chatText']);
                        break;

                        case 'getUsers':
                                $response = Chat::getUsers();
                        break;

                        case 'getChats':
                                $response = Chat::getChats($_GET['lastID']);
                        break;

                        default:
                                throw new Exception('Wrong action');
                }
                $this->set('response',$response);
        }
        catch(Exception $e){
                die(json_encode(array('error' => $e->getMessage())));
        }
        
    }
    
    
}

?>
